//
//  ByRechargeVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/5.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByRechargeVC.h"
#import "ByRechargeModuleView.h"
#import "ByMoneyChangeDetailVC.h"
#import "ByRechargePayBombView.h"


@interface ByRechargeVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) ByRechargePayBombView *payBombView;
@property (nonatomic, strong) ByNetModel *orderModel;
@property (nonatomic, assign) NSInteger type;

@end

@implementation ByRechargeVC

- (ByRechargePayBombView *)payBombView{
    if (_payBombView == nil) {
        _payBombView = [[ByRechargePayBombView alloc] init];
        __weak typeof(self) weakSelf = self;
        _payBombView.sureBlock = ^(){
               [weakSelf.payBombView remove];
                weakSelf.payBombView = nil;
                NSArray *array  = [ByCacheUser getPayStyle];
                NSDictionary *dic = [array objectAtIndex:weakSelf.type];
            
                NSString *can_use = dic[@"can_use"];
                if (can_use.integerValue != 1) {
                    [ByCoreToolAlert showOnlyText:@"暂不支持此支付方式"];
                    return;
                }
                 NSString *pay_type = dic[@"pay_type"];
                 weakSelf.orderModel.pay_type = pay_type;
                [ByMoneyApi fsPayRecharge:weakSelf.orderModel success:^(ByNetBaseResult *result) {
            
                    NSString *str = result.data[@"data"];
                    if ([pay_type isEqualToString:@"FSB0115"] || [pay_type isEqualToString:@"GR_ALIPAY_01"]) {
                        
                        ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:str];
                        vc.titleString = @"支付";
                        [weakSelf by_normalPush:vc hasNavi:YES];
                        
                    }else{
                        
                        ByWechatPayQrView *vc = [[ByWechatPayQrView alloc] init];
                        vc.title = @"微信支付";
                        vc.str = str;
                        [weakSelf by_normalPush:vc hasNavi:YES];
                    }
                    
                        
                    } failure:^(ByNetBaseResult *result) {
                        
                        [ByCoreToolAlert showFailureWithText:result.msg];
                        
                    }];
        };
        _payBombView.selectBlock = ^(NSInteger index){
            weakSelf.type = index;
        };
    }
    return _payBombView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    
    self.title = @"充值";
    self.index = 0;
    [self initViews];
    
    [self addBYItemviews:@[@"充值明细"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.type = 0;
}

- (void)rightItemsAction:(NSInteger)rightIndex{
    ByMoneyChangeDetailVC *vc = [[ByMoneyChangeDetailVC alloc] init];
    vc.type = 1;
    [self by_normalPush:vc hasNavi:YES];
}

- (void)initViews{
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(0);
        }
        
        
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    UILabel *titleLabel = [[UILabel alloc] init];
    [headView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headView).offset(10);
        make.right.equalTo(headView).offset(-10);
        make.centerY.equalTo(headView).offset(0);
        make.height.mas_equalTo(@20);
    }];
    
    titleLabel.font = FONT(16);
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = @"充值金额：";
    
    self.tableView.tableHeaderView = headView;
    
    
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    footView.backgroundColor = BY_BACKGROUND_COLOR;
    
    UIButton *rechargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [footView addSubview:rechargeBtn];
    [rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(footView).offset(0);
        make.top.equalTo(footView).offset(100);
        make.height.mas_equalTo(@39);
        make.width.mas_equalTo(@270);
    }];
    rechargeBtn.backgroundColor = BY_THEME_COLOR;
    [rechargeBtn setTitle:@"立即充值" forState:UIControlStateNormal];
    [rechargeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rechargeBtn addTarget:self action:@selector(rechargeAction:) forControlEvents: UIControlEventTouchUpInside];
    self.tableView.tableFooterView = footView;
    
    
    
    
}

- (void)rechargeAction:(UIButton *)btn{
    
    NSArray *array  = [ByCacheUser getPayStyle];
    if (!array || array.count == 0) {
        [ByCoreToolAlert showOnlyText:@"暂无可用支付方式"];
        return;
        
    }
   
    NSArray *itemsAry = @[@"10",@"50",@"100",@"500",@"1000",@"5000",@"10000",@"20000"];
    self.orderModel = [[ByNetModel alloc] init];
    self.orderModel.money = [NSString stringWithFormat:@"%@", itemsAry[self.index]];
    self.payBombView.moneyLabel.text = [NSString stringWithFormat:@"%@元", self.orderModel.money];
    self.payBombView.orderModel = self.orderModel;
    [self.payBombView show];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        static NSString *cellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            
            UILabel *line = [[UILabel alloc] init];
            [cell addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell).offset(0);
                make.right.equalTo(cell).offset(0);
                make.bottom.equalTo(cell).offset(0);
                make.height.mas_equalTo(@1);
            }];
            line.backgroundColor = BY_LINE_COLOR;
            
        }
        for (UIView *view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }
        
        ByRechargeModuleView *view = [[ByRechargeModuleView alloc] init];
        [cell.contentView addSubview:view];
        [view  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).offset(0);
            make.top.equalTo(cell.contentView).offset(0);
            make.right.equalTo(cell.contentView).offset(0);
            make.bottom.equalTo(cell.contentView).offset(0);
        }];
        view.dataArray = @[@"10",@"50",@"100",@"500",@"1000",@"5000",@"10000",@"20000"];;
        view.selectValueBlock = ^(NSInteger index){
            self.index = index;
        };
        
        return cell;
        
    }else{
        
        
        static NSString *cellIdentifier = @"cell111";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            
            UILabel *line = [[UILabel alloc] init];
            [cell addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell).offset(0);
                make.right.equalTo(cell).offset(0);
                make.bottom.equalTo(cell).offset(0);
                make.height.mas_equalTo(@1);
            }];
            line.backgroundColor = BY_LINE_COLOR;
            
        }
        for (UIView *view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }
        
//        UIImageView *typeView = [[UIImageView alloc] init];
//        [cell.contentView addSubview:typeView];
//        [typeView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(cell.contentView).offset(10);
//            make.centerY.equalTo(cell.contentView).offset(0);
//            make.size.mas_equalTo(CGSizeMake(40, 40));
//        }];
//        typeView.image = [UIImage imageNamed:@"weixin"];
//
//        UILabel *titleLabel = [[UILabel alloc] init];
//        [cell.contentView addSubview:titleLabel];
//        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(typeView.mas_right).offset(10);
//            make.centerY.equalTo(cell.contentView).offset(0);
//            make.size.mas_equalTo(CGSizeMake(120, 20));
//        }];
//
//        titleLabel.font = FONT(14);
//        titleLabel.textColor = [UIColor blackColor];
//        titleLabel.text = @"微信支付";
//
//        UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [cell.contentView addSubview:selectBtn];
//        [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.equalTo(cell.contentView).offset(-10);
//            make.centerY.equalTo(cell.contentView).offset(0);
//            make.size.mas_equalTo(CGSizeMake(20, 20));
//        }];
//        [selectBtn setImage:[UIImage imageNamed:@"morendizhixuanzhong"] forState:UIControlStateNormal];
        
        return cell;
    }
   
   
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 100 + 20;
    }else{
        return 0.01;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
